﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;

namespace Reconciliation.Data
{
    public partial class Case
    {
        public static Domain.Case MapDataToDomain(Data.Case dataCase)
        {            
            Domain.CaseAuditorComment domainCaseAuditorComment = null;
            Domain.CaseClientComment domainCaseClientComment = null;
            Domain.CaseAttachment domainCaseAttachment = null;
            Domain.Case domainCase = new Reconciliation.Domain.Case();
            domainCase.CaseCode = dataCase.CaseCode ;
            domainCase.CaseDate = dataCase.CaseDate ;
            domainCase.CaseID = dataCase.CaseID ;
            domainCase.Status = dataCase.Status ;            
            domainCase.StatusDate = dataCase.StatusDate;           
            foreach (Data.CaseAuditorComment cac in dataCase.CaseAuditorComment)
            {
                domainCaseAuditorComment = Data.CaseAuditorComment.MapDataToDomain(cac);
                domainCase.CaseAuditorCommentList.Add(domainCaseAuditorComment);
            }
            foreach (Data.CaseClientComment ccc in dataCase.CaseClientComment)
            {
                domainCaseClientComment = Data.CaseClientComment.MapDataToDomain(ccc);
                domainCase.CaseClientCommentList.Add(domainCaseClientComment);
            }
            foreach (Data.CaseAttachment ca in dataCase.CaseAttachment)
            { 
                domainCaseAttachment = Data.CaseAttachment.MapDataToDomain(ca);
                domainCase.CaseAttachmentList.Add(domainCaseAttachment);
            }
            return domainCase;
        }

        public static Domain.Case GetCase(int orderID)
        {
            try
            {
                Domain.Case domainCase = null;
                Data.Case dataCase = null;
                ReconciliationEntities recEnt = new ReconciliationEntities();
                int caseID = recEnt.Order.Where(o => o.OrderID.Equals(orderID)).Select(o => o.Case.CaseID).FirstOrDefault();
                dataCase = recEnt.Case.Include("Order_1").Include("Order_1.OrderDetail").Where(c => c.CaseID.Equals(caseID)).FirstOrDefault();
                if (dataCase != null)
                {
                    domainCase = Data.Case.MapDataToDomain(dataCase);
                }
                return domainCase;
            }
            catch (Exception ex)
            {
                Log.LogEntryOnException("GetCase", ex);
                return new Reconciliation.Domain.Case();

            }
        }

        public static Domain.Case UpdateCase(Domain.Case domainCase)
        {
            try
            {
                using (ReconciliationEntities recEnt = new ReconciliationEntities())
                {
                    Data.Case dataCase = recEnt.Case.Where(c => c.CaseID.Equals(domainCase.CaseID)).FirstOrDefault();
                    dataCase.Status = domainCase.Status;
                    recEnt.SaveChanges();
                    domainCase = MapDataToDomain(dataCase);
                    return domainCase;
                }
            }
            catch (Exception ex)
            {
                ExceptionPolicy.HandleException(ex, "Exception Policy");
                throw new Domain.ReconciliationException("Unable to update status");
            }
        }
    }
}
